package com.morontestandroid;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.TextView;



public class TenthLevel extends Activity{
    private SensorManager mSensorManager;
    private float mAccel; // acceleration apart from gravity
    private float mAccelCurrent; // current acceleration including gravity
    private float mAccelLast; // last acceleration including gravity
    Vibrator v;
	AnimationDrawable AniFrame;
	AnimationDrawable AniFrame2;
	ImageView MyImageView;
	ImageView warioImage;
	TextView timer;
	TextView shake;
	int adder=1;
	boolean animationBo1=false;
	boolean animationBo2=false;
	boolean animationBo3=false;
	boolean animationBo4=false;
	boolean startBo=false;
	boolean win=false;
	boolean lose=false;
	Chronometer mChronometer;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tenth_level);
      
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
        mAccel = 0.00f;
        mAccelCurrent = SensorManager.GRAVITY_EARTH;
        mAccelLast = SensorManager.GRAVITY_EARTH;
        v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        
        countDown();                          // CALL THE METHOD IN onCreate
        timer = (TextView) findViewById(R.id.textView3);
        shake = (TextView) findViewById(R.id.textView2);
        warioImage = (ImageView)findViewById(R.id.warioImage);
        warioImage.setBackgroundResource(R.drawable.run_animation2);
        MyImageView = (ImageView)findViewById(R.id.myImageView);
        MyImageView.setBackgroundResource(R.drawable.run_animation);
        mChronometer = (Chronometer) findViewById(R.id.level10ChrmId);
        
        mChronometer.setBase(getTime());          
		mChronometer.start();
        
		AniFrame = (AnimationDrawable) MyImageView.getBackground();
		AniFrame2 = (AnimationDrawable) warioImage.getBackground();
     
	       TextView txt = (TextView) findViewById(R.id.textView1);  
	       Typeface font = Typeface.createFromAsset(getAssets(), "testtext.ttf");  
	        txt.setTypeface(font);
    }
    
    public long getTime() {
		  Bundle extras = getIntent().getExtras();
		  long time = extras.getLong("time");
		  return time;
		 
	}
    
    public void countDown(){
    
    	final Handler handler = new Handler();
    	handler.postDelayed(new Runnable() {
      	  @Override
      	  public void run() {
      		  timer.setText("3");
      	  }
      	}, 0); 
    	handler.postDelayed(new Runnable() {
    	  @Override
    	  public void run() {
		  timer.setText("2");
    	  }
    	}, 1000);         //  <<<<<<<<<<<<<   TIME FOR THE DELAY
    	handler.postDelayed(new Runnable() {
      	  @Override
      	  public void run() {
        timer.setText("1");
      	  }
      	}, 2000); 
    	handler.postDelayed(new Runnable() {
      	  @Override
      	  public void run() {
         	timer.setVisibility(View.GONE);
      		  shake.setVisibility(View.VISIBLE);
      		AniFrame.start();
      		AniFrame2.start();
      		startBo=true;
      		animationBo1=true;
      		wario1();
      	
      		
      	  }
      	}, 3000); 
    	handler.postDelayed(new Runnable() {
        	  @Override
        	  public void run() {

        		shake.setVisibility(View.GONE);
        	  }
        	}, 5000); 
    }
    
    private final SensorEventListener mSensorListener = new SensorEventListener() {

        public void onSensorChanged(SensorEvent se) {
          float x = se.values[0];
          float y = se.values[1];
          float z = se.values[2];
          mAccelLast = mAccelCurrent;
          mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
          float delta = mAccelCurrent - mAccelLast;
          mAccel = mAccel * 0.9f + delta; // perform low-cut filter
          isItShaken();
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
      };

      @Override
      protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
      }
      public void isItShaken(){
          if(mAccel>=2 && startBo==true){
        	  adder=adder+1;
        	   	v.vibrate(100);
          }

          if(adder==3 && animationBo1==true){
          animation();
          }
          if(adder==3 && animationBo2==true){
          animation2();
          }
          if(adder==3 && animationBo3==true){
          animation3();
          }
          if(adder==3 && animationBo4==true){
          animation4();
          }

      }

      @Override
      protected void onStop() {
        mSensorManager.unregisterListener(mSensorListener);
        super.onStop();
      }
    
    
    public void animation(){
    	Animation firstAni = new TranslateAnimation(0, 85, 0, 0);
	        firstAni.setDuration(2000);
	        
	        firstAni.setAnimationListener(new AnimationListener(){

	            public void onAnimationStart(Animation animation) {
	            // do nothing       
	            }
	            public void onAnimationRepeat(Animation animation) {
	            // do nothing       
	            }
                  // at the end of the animation
	            public void onAnimationEnd(Animation animation) {
	            	setLocationMario(85,232+120);
	            	animationBo2=true;
	            	animationBo1=false;
	            	 adder=1;
	            }
	});
	        MyImageView.startAnimation(firstAni);
	       
		 }
    public void animation2(){
    	Animation firstAni = new TranslateAnimation(0, 85, 0, 0);
	        firstAni.setDuration(2000);
	        
	        firstAni.setAnimationListener(new AnimationListener(){

	            public void onAnimationStart(Animation animation) {
	            // do nothing       
	            }
	            public void onAnimationRepeat(Animation animation) {
	            // do nothing       
	            }
                  // at the end of the animation
	            public void onAnimationEnd(Animation animation) {
	            	setLocationMario(85+85,232+120);
	            	animationBo2=false;
	            	animationBo3=true;
	            	 adder=1;
	            }
	});
	        MyImageView.startAnimation(firstAni);
		 }
    public void animation3(){
    	Animation firstAni = new TranslateAnimation(0, 85, 0, 0);
	        firstAni.setDuration(2000);
	        
	        firstAni.setAnimationListener(new AnimationListener(){

	            public void onAnimationStart(Animation animation) {
	            // do nothing       
	            }
	            public void onAnimationRepeat(Animation animation) {
	            // do nothing       
	            }
                  // at the end of the animation
	            public void onAnimationEnd(Animation animation) {
	            	setLocationMario(85+85+85,232+120);
	            	animationBo3=false;
	            	animationBo4=true;
	            	 adder=1;
	            }
	});
	        MyImageView.startAnimation(firstAni);
		 }
    public void animation4(){
    	Animation firstAni = new TranslateAnimation(0, 85, 0, -200);
	        firstAni.setDuration(2000);
	        
	        firstAni.setAnimationListener(new AnimationListener(){

	            public void onAnimationStart(Animation animation) {
	            // do nothing       
	            }
	            public void onAnimationRepeat(Animation animation) {
	            // do nothing       
	            }
                  // at the end of the animation
	            public void onAnimationEnd(Animation animation) {
	            //	setLocation(340,32+120);
	            if(lose==false){
	            	win=true;
	            	animationBo4=false;
	            	 shake.setText("Victory!");
	            	shake.setVisibility(View.VISIBLE);
	            }
	            }
	});
	        MyImageView.startAnimation(firstAni);
	   	 firstAni.setFillAfter(true);
		 }
    
    public void winAnimation(){
    	final Handler handler = new Handler();
    	handler.postDelayed(new Runnable() {
      	  @Override
      	  public void run() {
			/*	int activityID = 1;
				Intent intent;
				intent = new Intent().setClass(this, SixthLevel.class);
				intent.putExtra("time", getThisBase());
				startActivityForResult(intent, activityID);
				*/
      	  }
      	}, 4000); 
    }
    public void wario1(){
    	Animation firstAni = new TranslateAnimation(0, -290, 0, 0);
	        firstAni.setDuration(15000);
	        
	        firstAni.setAnimationListener(new AnimationListener(){

	            public void onAnimationStart(Animation animation) {
	            // do nothing       
	            }
	            public void onAnimationRepeat(Animation animation) {
	            // do nothing       
	            }
                  // at the end of the animation
	            public void onAnimationEnd(Animation animation) {
	            	setLocationWario(460,232+115);
	            	wario2();
	            }
	});
	        warioImage.startAnimation(firstAni);
	   
		 }
    public void wario2(){
    	Animation firstAni = new TranslateAnimation(0, -77, 0, -195);
	        firstAni.setDuration(4000);
	        
	        firstAni.setAnimationListener(new AnimationListener(){

	            public void onAnimationStart(Animation animation) {
	            // do nothing       
	            }
	            public void onAnimationRepeat(Animation animation) {
	            // do nothing       
	            }
	            
	            // at the end of the animation
	            public void onAnimationEnd(Animation animation) {
	            	if(win==false){
	            		endOfLevel();
	            		//lose=true;
	            		//shake.setText("Game Over!");
	            		//shake.setVisibility(View.VISIBLE);
	         }
	            }
	});
	        warioImage.startAnimation(firstAni);
	      	 firstAni.setFillAfter(true);
		 }
    
    
	private void setLocationMario(int x, int y) {
	    final ImageView iv = (ImageView) findViewById(R.id.myImageView);
	    final AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) iv.getLayoutParams();
	    lp.x = x;
	    lp.y = y;
	    iv.setLayoutParams(lp);
	 
	}
	private void setLocationWario(int x, int y) {
	    final ImageView iv = (ImageView) findViewById(R.id.warioImage);
	    final AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) iv.getLayoutParams();
	    lp.x = x;
	    lp.y = y;
	    iv.setLayoutParams(lp);
	 
	}
	
	private void endOfLevel(){
		int activityID = 1;
		Intent intent;
		intent = new Intent().setClass(this, CheckPoint.class);
		intent.putExtra("time", getThisBase());
		startActivityForResult(intent, activityID);
		 //startActivity(new Intent(this, CheckPoint.class));
	}
	
	public long getThisBase(){
		  return mChronometer.getBase();
		 
	}
	public void onBackPressed() {
		// do something on back.
		startActivity(new Intent(this, MenyActivity.class));
		return;
		}
	
}
